package me.flyray.bsin.server.biz;

import lombok.extern.slf4j.Slf4j;
import me.flyray.bsin.domain.request.AppChatDTO;
import me.flyray.bsin.exception.BusinessException;
import me.flyray.bsin.server.ws.WsServerEndpoint;

/**
 * @author leonard
 * @description
 * @createDate 2023/12/2023/12/28 /14/08
 */
@Slf4j
public class WebSocketAiChatAsyncBiz implements Runnable {

  private final AppChatDTO aiChatDTOReq;
  private final WsServerEndpoint webSocketServer;
  private final AppAgentEngine appAgentEngine;

  public WebSocketAiChatAsyncBiz(
          AppChatDTO aiChatDTOReq, WsServerEndpoint webSocketServer, AppAgentEngine chatBiz) {
    this.aiChatDTOReq = aiChatDTOReq;
    this.webSocketServer = webSocketServer;
    this.appAgentEngine = chatBiz;
  }

  @Override
  public void run() {
    try {
      log.info("智能体处理开始 - Question: {}", aiChatDTOReq.getQuestion());
      this.appAgentEngine.chat(this.aiChatDTOReq, this.webSocketServer);
      log.info("智能体处理完成");
    } catch (Exception e) {
      log.error("智能体处理异常", e);
      this.webSocketServer.sendMessageAsync("{\"type\":\"error\",\"message\":\"处理失败: " + e.getMessage() + "\"}");
    }
  }
}
